package com.zsk.threadpool;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledExecutorService;

/**
 * 线程池，本质上都是new TheadPoolExcutror
 * 
 * @author zincredible
 * @date 2019/09/21 16:15:40
 */
public class ThreadPool {

	/**
	 * 定长线程池
	 */
	public static void fixThead() {
		// 创建定长为5的线程池，只有5个线程同时处理任务
		ExecutorService threadPool = Executors.newFixedThreadPool(5);

		try {
			for (int i = 0; i < 20; i++) {
				threadPool.execute(() -> {
					System.out.println(Thread.currentThread().getName() + "：办理业务");
				});
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			threadPool.shutdown();
		}
	}

	/**
	 * 单线程池
	 */
	public static void singleThead() {
		// 单线程池，只有一个线程处理任务
		ExecutorService threadPool = Executors.newSingleThreadExecutor();

		try {
			for (int i = 0; i < 20; i++) {
				threadPool.execute(() -> {
					System.out.println(Thread.currentThread().getName() + "：办理业务");
				});
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			threadPool.shutdown();
		}

	}

	/**
	 * 可缓存的线程池，
	 */
	public static void cacheThead() {
		// 缓存线程池，有N个线程处理，N大小跟硬件支持有关
		ExecutorService threadPool = Executors.newCachedThreadPool();

		try {
			for (int i = 0; i < 20; i++) {
				threadPool.execute(() -> {
					System.out.println(Thread.currentThread().getName() + "：办理业务");
				});
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			threadPool.shutdown();
		}

	}

	/**
	 * 可缓存的线程池，
	 * 
	 * @throws ExecutionException
	 * @throws InterruptedException
	 */
	public static void sceduleThead() throws InterruptedException, ExecutionException {
		// 缓存线程池，有N个线程处理，N大小跟硬件支持有关
		ScheduledExecutorService threadPool = Executors.newScheduledThreadPool(5);
		try {
			Callable<String> callable = new Callable<String>() {
				@Override
				public String call() throws Exception {
					Thread.sleep(3000);
					System.out.println("calld方法执行了");
					return "call方法返回值";
				}
			};
			Future<String> future = threadPool.submit(callable);
			System.out.println("提交任务之后，获取结果之前 " + getStringDate());
			System.out.println("获取返回值: " + future.get());
			System.out.println("获取到结果之后 " + getStringDate());
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			threadPool.shutdown();
		}

	}

	public static String getStringDate() {
		Date currentTime = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
		String dateString = formatter.format(currentTime);
		return dateString;
	}

	public static void main(String[] args) throws InterruptedException, ExecutionException {
		// fixThead();
		// singleThead();
		// cacheThead();
		sceduleThead();
	}

}
